//
//  PKLoginViewController.m
//  PianKe
//
//  Created by lanou on 15/12/24.
//  Copyright © 2015年 yht. All rights reserved.
//

#import "PKLoginViewController.h"
#import "MqRegisterViewController.h"
#import "UMSocial.h"
#import "MqMyViewController.h"
#import "MqInfoSearchTableViewCell.h"
@interface PKLoginViewController ()

@property (nonatomic, strong) MqMyViewController *myVC;

@end

@implementation PKLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _myVC = [[MqMyViewController alloc] init];
    [self addChildViewController:_myVC];
    
    [self.view addSubview:_myVC.view];
    
    
    // 初始化登陆视图
    _login = [[PKLoginView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [self.view addSubview:_login];
    _login.backgroundColor = [UIColor whiteColor];
    
 
    
    //  记住密码
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"rmbpwd"]) {
        
        _login.accountText.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"account"];
        
        _login.pwdText.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"pwd"];
        _login.rmbSwitch.on = YES;
       
      
    }
    [_login.accountText addTarget:self action:@selector(textChange) forControlEvents:(UIControlEventEditingChanged)];
    [_login.pwdText addTarget:self action:@selector(textChange) forControlEvents:(UIControlEventEditingChanged)];
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"autoLogin"]) {
        
        _login.loginSwitch.on = YES;
    }
    
    
    //  自动登录
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"autoLogin" ] == YES) {
        
        //  自动登录
        [self loginButton];
  
    }
    
    //  记住密码与自动登录事件
    [_login.rmbSwitch addTarget:self action:@selector(rmbpSwitch:) forControlEvents:UIControlEventValueChanged];
    
    [_login.loginSwitch addTarget:self action:@selector(loginSwitch:) forControlEvents:UIControlEventValueChanged];
    
    //注册按钮的点击事件
    [_login.registerButton addTarget:self action:@selector(registerButtonClick:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    //  登陆按钮的点击事件
    [_login.loginButton addTarget:self action:@selector(loginButton) forControlEvents:(UIControlEventTouchUpInside)];

    
    //隐藏navigationBar
    self.navigationController.navigationBar.hidden = YES;
    
    
    _sinaLoginButton = (UIButton *)[ _login viewWithTag:603];
    [_sinaLoginButton addTarget:self action:@selector(renrenClick:) forControlEvents:(UIControlEventTouchUpInside)];
}

- (void)textChange{
    
    if (_login.accountText.text.length && _login.pwdText.text.length) {
        
        _login.loginSwitch.enabled = YES;
        _login.rmbSwitch.enabled = YES;
        
    }else{
        
        _login.loginSwitch.enabled = NO;
        _login.rmbSwitch.enabled = NO;
    }
    
}

//  取消记住密码,同时取消自动登录
- (void)rmbpSwitch:(UISwitch *)mySwitch{
    
    if (mySwitch.isOn == NO) {
        
        //  取消记住密码 取消自动登录 以及已经记住的密码
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"rmbpwd"];
        
        
        [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"account"];
         [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"autoLogin"];
        [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"pwd"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        [_login.loginSwitch setOn:NO animated:YES];
        
        
    }else{
        
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"rmbpwd"];
        
        //  记住密码
        [[NSUserDefaults standardUserDefaults] setObject:_login.accountText.text forKey:@"account"];
        [[NSUserDefaults standardUserDefaults] setObject:_login.pwdText.text forKey:@"pwd"];
        
        [[NSUserDefaults standardUserDefaults] synchronize];

    }
    
}

//  点击自动登陆同时记住密码
- (void)loginSwitch:(UISwitch *)mySwitch{
    
    if (mySwitch.isOn == YES) {
        
        [_login.rmbSwitch setOn:YES animated:YES];
        
         [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"autoLogin"];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"rmbpwd"];
        [[NSUserDefaults standardUserDefaults] synchronize];

        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"rmbpwd"];
        
        //  记住密码
        [[NSUserDefaults standardUserDefaults] setObject:_login.accountText.text forKey:@"account"];
        [[NSUserDefaults standardUserDefaults] setObject:_login.pwdText.text forKey:@"pwd"];
        
        [[NSUserDefaults standardUserDefaults] synchronize];
        
    }else{
        
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"autoLogin"];
        [[NSUserDefaults standardUserDefaults] synchronize];

    }
    
}

//  登陆
- (void)loginButton{

    NSString *loginString = [NSString stringWithFormat:@"http://localhost/login.php?account=%@&password=%@", _login.accountText.text, _login.pwdText.text];
    //    NSLog(@"%@",_login.pwdText.text);
    
    [PKRequestManager requestWithUrlString:loginString parDic:nil Method:GET finish:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
        
        
        NSString *msg = @"";
        
        //  空
        if (!dic ||  [dic isKindOfClass:[NSNull class]]) {
            msg = @"该账号尚未注册";
            
        }else if ([dic[@"state"] isEqualToString:@"true"]){
            
            msg = @"登陆成功";
            
        }else{
            
            msg = @"账号或密码错误";
        }
       
      
        
        //  提示框
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        
        [alert show];
        
        //  提示两秒后关闭
        [self performSelector:@selector(loginSuccess:) withObject:alert afterDelay:1];
        
    } error:^(NSError *error) {
        
        
    } path:nil ];
    
}

//  登陆成功
- (void)loginSuccess:(UIAlertView *)alert{
    
    [alert dismissWithClickedButtonIndex:0 animated:YES];
    
    if ([alert.message isEqualToString:@"登陆成功"]) {
        
        _myVC.nameLabel.text = _login.accountText.text;
        [self.view bringSubviewToFront:_myVC.view];
        
        //  记住密码
        [[NSUserDefaults standardUserDefaults] setObject:_login.accountText.text forKey:@"account"];
        [[NSUserDefaults standardUserDefaults] setObject:_login.pwdText.text forKey:@"pwd"];
        
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    
}
//  注册
- (void)registerButtonClick:(UIButton *)button
{
    
    MqRegisterViewController *registerVC = [[MqRegisterViewController alloc] init];
    
    [self presentViewController:registerVC animated:YES completion:nil];
    
}

- (void)renrenClick:(UIButton *)renren
{
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //  获取微博用户名、uid、token等
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            _myVC.nameLabel.text = snsAccount.userName;
            [_myVC.imageButton sd_setBackgroundImageWithURL:[NSURL URLWithString:snsAccount.iconURL] forState:(UIControlStateNormal)];
            
            [self.view bringSubviewToFront:_myVC.view];
            //  记住密码
            [[NSUserDefaults standardUserDefaults] setObject:_login.accountText.text forKey:@"account"];
            [[NSUserDefaults standardUserDefaults] setObject:_login.pwdText.text forKey:@"pwd"];
            
            [[NSUserDefaults standardUserDefaults] synchronize];

        }});
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
